package com.future.mayi.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;

import com.future.baselib.utils.CommonUtils;
import com.future.mayi.R;
import com.future.mayi.fragment.PhotoFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 周大虎 on 2017/10/9.
 */

public class PhotoActivity extends FragmentActivity implements View.OnClickListener {

    private ViewPager viewPager;
    private int setCurrentItem;

    private List<PhotoFragment> mFragment = new ArrayList<>();

    private LinearLayout llTab;
    private List<View> views;
    private int selectBg = R.drawable.photo_tab_bg_t;
    private int viewBg = R.drawable.photo_tab_bg_f;
    private ArrayList<String> listUrl;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getIntent() == null) {
            finish();
            return;
        }


        listUrl = getIntent().getStringArrayListExtra("listUrl");
        setCurrentItem = getIntent().getIntExtra("setCurrentItem", 0);

        if (listUrl == null || listUrl.size() == 0) {
            finish();
            return;
        }

        setContentView(R.layout.activity_photo);
        initView();


    }

    private void initView() {
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        llTab = (LinearLayout) findViewById(R.id.ll_tab);
        viewPager = (ViewPager) findViewById(R.id.view_pager);

        View view;
        views = new ArrayList<>();
        LinearLayout.LayoutParams layoutParams;
        for (int i = 0; i < listUrl.size(); i++) {
            PhotoFragment photoFragment = new PhotoFragment();
            mFragment.add(photoFragment);
            view = new View(this);
            layoutParams = new LinearLayout.LayoutParams(CommonUtils.dip2px(this, 4), CommonUtils.dip2px(this, 4));
            layoutParams.setMargins(CommonUtils.dip2px(this, 4), 0, CommonUtils.dip2px(this, 4), 0);
            llTab.addView(view);
            view.setLayoutParams(layoutParams);
            view.setBackgroundResource(viewBg);
            views.add(view);
        }

        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                mFragment.get(position).setData(listUrl.get(position), position, PhotoActivity.this);
                return mFragment.get(position);
            }

            @Override
            public int getCount() {
                return mFragment.size();
            }
        });


        if (setCurrentItem != 0 && mFragment.size() > setCurrentItem)
            viewPager.setCurrentItem(setCurrentItem);


        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                for (int i=0;i<views.size();i++){
                    if (position==i){
                        views.get(i).setBackgroundResource(selectBg);
                    }else {
                        views.get(i).setBackgroundResource(viewBg);
                    }
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    @Override
    public void onClick(View v) {
        this.finish();
    }
}
